﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace Applay.Web
{
    public class ModelValidationState : IValidationState
    {
        private readonly ModelStateDictionary modelState;

        public ModelValidationState(ModelStateDictionary modelState)
        {
            Contract.Requires(modelState != null);

            this.modelState = modelState;
        }

        public void AddError(string message, params string[] keys)
        {
            if (!keys.Any())
            {
                this.modelState.AddModelError(string.Empty, message);
            }
            else
            {
                foreach (var key in keys)
                {
                    this.modelState.AddModelError(key ?? string.Empty, message);
                }
            }
        }

        public bool IsValid
        {
            get { return this.modelState.IsValid; }
        }
    }
}
